package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import modelo.Aluno;

import util.DataManager;

public class daoAluno {
	public static ArrayList<Aluno> getAlunos(DataManager dataManager) {
		ArrayList<Aluno> alunos = new ArrayList<Aluno>();
		Connection conn = dataManager.getConnection();
		
		if ( conn != null ) {
			// first create the statement
			try {
				Statement s = conn.createStatement();
				String sql = "select * from aluno";
			// execute the statement
			try { 
				ResultSet rs = s.executeQuery(sql);
				
				// get the results
				try {
 
					while ( rs.next() ) {
						// instantiate a new Aluno object
						Aluno a = new Aluno();
						
						// set the attributes using the resultset
						a.setCodaluno ( rs.getInt("codaluno") );
						a.setNmaluno  ( rs.getString("nmaluno") );
						a.setMatricula( rs.getString("matricula") );
						a.setEmail    ( rs.getString("email") );
						a.setTelefone ( rs.getString("telefone") );
						a.setSenha    ( rs.getString("senha") );
						a.setAprovado ( rs.getBoolean("aprovado") );
						
						// add the Aluno object to the collection
						alunos.add(a);
						
					
					} // while
				} finally {
					// after using the result set we close it
					rs.close();
				} 
			} 
			finally {
				// and after using the statement we close it too
				s.close();
			}
				
			// catch any exception 
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		} // if
		
		// return the collection
		return alunos;
	}
}
